<?php

/**
 * This is the model class for table "{{adm_role}}".
 *
 * The followings are the available columns in table '{{adm_role}}':
 * @property integer $zao_id
 * @property string $zao_title
 * @property string $zao_content
 * @property integer $zao_type
 * @property integer $zao_stat
 * @property integer $inputtime
 * @property integer $updatetime
 */
class AdmRole extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdmRole the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{adm_role}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zao_type, zao_stat, inputtime, updatetime,zao_id', 'numerical', 'integerOnly'=>true),
			array('zao_title', 'length', 'max'=>100),
			array('zao_content', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zao_id, zao_title, zao_content, zao_type, zao_stat, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'actions'=>array(self::HAS_MANY, 'AdmPrivilege', array('parent'=>'zao_id')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zao_id' => 'Zao',
			'zao_title' => 'Zao Title',
			'zao_content' => 'Zao Content',
			'zao_type' => 'Zao Type',
			'zao_stat' => 'Zao Stat',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zao_id',$this->zao_id);
		$criteria->compare('zao_title',$this->zao_title,true);
		$criteria->compare('zao_content',$this->zao_content,true);
		$criteria->compare('zao_type',$this->zao_type);
		$criteria->compare('zao_stat',$this->zao_stat);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " inputtime DESC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 保存前的操作
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->inputtime = time();
		}else{
			$this->updatetime = time();
		}
		return true;
	}
	
	/**
	 * 管理员角色列表
	 */
	public function getRoleListData($type=null){
		if($type==null){
			$roles = $this->findAll();
			return CHtml::listData($roles,'zao_id','zao_title');
		}else{
			$data = $this->getRoleListData();
			if(array_key_exists($type,$data))
				return $data[$type];
		}
	}
}